<?php
namespace Admin\Model;
use Think\Verify;
use Think\Model;
class UserModel extends Model 
{

	public $tableName = 'user';

	/*登录验证*/
	public function checkUse($phone, $pwd)
	{
		$space = preg_match('/^0?(13|14|15|17|18)[0-9]{9}$/', $phone);
		
		if($space)
		{
			$userModel    = M($this->tableName);
			$use['phone'] = $phone;
			$use['pwd']   = $pwd;
			$res          = $userModel->where($use)->limit(1)->select();
			
			return $res;
		}else
		{
			return 0;
		}

	}
	/*注册*/
	public function insertUse($phone, $pwd, $pid, $name)
	{
		$space = preg_match('/^0?(13|14|15|17|18)[0-9]{9}$/', $phone);

		if($space)
		{
			$userModel = M('user');

			$use['phone'] = $phone;
			$use['pwd'] = $pwd;
        	$use['name'] = $name;
			$use['pid'] = $pid;
			$user['phone'] = $phone;
			$res = $userModel->where($user)->select();
			if($res)
			{
				return 'one';
			}else
			{
				/*推广佣金*/
				//$proxy = $this->proxy($use['pid']);
				/*推广佣金end*/
				$res_in = $userModel->data($use)->add();
				return $res_in;
				
			}
		}else
		{
			return 'phone';
		}
	}
	
	/*注册*/
	public function insertUse10($phone, $pwd, $name, $leverPhone, $leverType)
	{
		$space = preg_match('/^0?(13|14|15|17|18)[0-9]{9}$/', $phone);

		if($space)
		{
			$userModel = M('user');

			$user['phone'] = $phone;
			$res = $userModel->where($user)->select();
			if($res)
			{
				return 'one'; //会员已存在
			}else
			{
				if($leverPhone == '')
				{
					$use['phone'] = $phone;
					$use['pwd'] = $pwd;
					$use['name'] = $name;
					$res_in = $userModel->data($use)->add();
					return 'data'; //无上级
				}else
				{
					if($leverType == '会员')
					{
						$leverUser['phone'] = $leverPhone;
						$leverUserModel = M('user');
						$ures = $leverUserModel->where($leverUser)->select();
						if($ures)
						{
							$udata['phone'] = $phone;
							$udata['pwd'] = $pwd;
							$udata['name'] = $name;
							$udata['pid'] = $ures[0]['user_id'];
							$udata['is_user'] = 1;
							
							$res_udata = $userModel->data($udata)->add();
							return 'udata';   //上级是会员
						}else
						{
							return 'noLever';
						}
					}elseif($leverType == '商家')
					{
						$leverShop['phone'] = $leverPhone;
						$leverShop['apply'] = 1;
						$leverShop['isApply'] = 1;
						$leverShopModel = M('shop');
						$sres = $leverShopModel->where($leverShop)->select();
						//var_dump($sres);exit;
						if($sres)
						{
							$sdata['phone'] = $phone;
							$sdata['pwd'] = $pwd;
							$sdata['name'] = $name;
							$sdata['pid'] = $sres[0]['shop_id'];
							$sdata['is_user'] =0;
							
							$res_sdata = $userModel->data($sdata)->add();
							return 'sdata';   //上级是商家
						}else
						{
							return 'noLever';  //上级不存在
						}
					}
				}
				
				/*推广佣金*/
				//$proxy = $this->proxy($use['pid']);
				/*推广佣金end*/
				/*$res_in = $userModel->data($use)->add();
				return $res_in;*/
				
			}
		}else
		{
			return 'phone';
		}
	}

	/*注册1*/
	public function insertUse1($phone, $pwd, $pid, $is_user, $name)
	{
		$space = preg_match('/^0?(13|14|15|17|18)[0-9]{9}$/', $phone);

		if($space)
		{
			$userModel = M('user');

			$use['phone'] = $phone;
			$use['pwd'] = $pwd;
        	$use['name'] = $name;
			$use['pid'] = $pid;
			$use['is_user'] = $is_user;
			$user['phone'] = $phone;
			
			$res = $userModel->where($user)->select();
			if($res)
			{
				return 'one';
			}else
			{
				/*推广佣金*/
				//$proxy = $this->proxy($use['pid']);
				/*推广佣金end*/
				$res_in = $userModel->data($use)->add();
				return $res_in;
				
			}
		}else
		{
			return 'phone';
		}
	}
	/*推广返积分*/
	/*public function proxy($pid)
	{
		if($pid)
		{
			$configModel = M('config');
			$hasConfig = $configModel->where('Id = 1')->select();
			$commision = $hasConfig[0]['commision'];
			
			$shopModel = M('user');
			$hasShop = $shopModel->where('user_Id = '.$pid)->select();
			if($hasShop)
			{
				$logModel = M('log');
				$log['user_Id'] = $hasShop[0]['user_id'];
				$log['points'] = +0.006;
				$log['reason'] = '赠送积分';
				$log['is_tj'] = 1;
				$log['shijian'] = date('Y-m-d H:i:s',time());
				$log_in = $logModel->data($log)->add();
				if(!$log_in)
				{
					return no;
				}
			}
		}
		
    	
	}*/

	/*计算消费积分*/
	public function points($userid)
	{
		$logModel = M('log');
		$userModel = M('user');

		$arr['user_Id'] = $userid;
		$arr['reason'] = '消费积分';
		$res = $logModel->where($arr)->select();

		//定义一个变量，用于保存积分
		$log['pay_points'] = 0;
		for ($i=0; $i < count($res); $i++) { 
			$log['pay_points'] = $res[$i]['points'] + $log['pay_points'];
		}

		//计算有几个积分单元
		$weight = C('WEIGHT');
		$log['unit'] = floor($log['pay_points']/$weight);

		$Model = new \Think\Model();
		$new_point = $Model->execute("update user set pay_points=".$log['pay_points'].", unit =".$log['unit'].", isunit =".$log['unit']." where user_id = ".$arr['user_Id']);
		
		return $new_point;

	}

	/*计算返还积分*/
	public function returnPoints($userid)
	{
		$logModel = M('log');
		$userModel = M('user');

		$arr['user_Id'] = $userid;
		$resUser = $userModel->where($arr)->select();
		$arr['reason'] = '赠送积分';
		$res = $logModel->where($arr)->select();

		//定义一个变量，用于保存积分
		$log['return_points'] = $resUser[0]['return_points'];
		for ($i=0; $i < count($res); $i++) { 
			$log['return_points'] = $res[$i]['points'] + $log['return_points'];
		}

		$Model = new \Think\Model();
		$new_point = $Model->execute("update user set return_points=".$log['return_points']." where user_id = ".$arr['user_Id']);
		
		return $new_point;

	}

	/*获取你的店铺信息*/
	public function shop($userid)
	{
		$arr['user_Id'] = $userid;

		$userModel = M('user');
		$shopModel = M('shop');
		$new_user = $userModel->where($arr)->limit(1)->select();
		$shop['phone'] = $new_user[0]['phone'];
		$new_shop = $shopModel->where($shop)->select();

		return $new_shop;
	}

	/*获取你的下级*/
	public function myfans($userid, $tableName)
	{
		$res = array();
		$res_user = array();
		$arr['pid'] = $userid;

		$Model = M($tableName);
		$new_user = $Model->where($arr)->select();
		
		if($tableName == "shop"){
			foreach($new_user as $k => $v){
				$new_user[$k]['count_score'] = M('recharge') -> where(array('shop_id'=>$v['shop_id'],'reason'=>'购买积分')) -> sum('points');
				if($new_user[$k]['count_score'] == 0){
					$new_user[$k]['count_score'] = "0.00";
				}
			}
		}

		for ($i=0; $i < count($new_user); $i++) 
		{
			if ($new_user[$i]['is_user']) 
			{
				//获取下级
				$res_user[$i] = $new_user[$i];
			}
		}

		$res = $res_user;
		//var_dump($res);exit();
		return $res;
	}

	/*获取你的上级*/
	public function level($userid)
	{
		$arr['user_Id'] = $userid;
		$userModel = M('user');
		$new_user = $userModel->where($arr)->select();

		if($new_user[0]['is_user'])
		{
			$use['user_Id'] = $new_user[0]['pid'];

			$puser = $userModel->where($use)->select();
			if($puser)
			{
				$puser[0]['type'] = '会员';
			}
		}else
		{
			$shop['shop_id'] = $new_user[0]['pid'];

			$shopModel = M('shop');
			$puser = $shopModel->where($shop)->select();
			if($puser)
			{
				$puser[0]['type'] = '商家';
			}
		}
		return $puser;
	}

	/*修改密码*/
	public function pwd($arr)
	{
		$check = $arr['check'];
		$verify=new \Think\Verify();
        $res=$verify->check($check);

        $user['user_Id'] = $arr['user_Id'];
        $user['pwd'] = $arr['oldPwd'];
        $newPwd1 = $arr['newPwd1'];
        $use['pwd'] = $arr['newPwd2'];

        if($res)
        {
        	if($newPwd1 == $use['pwd'])
        	{
				$userModel = M($this->tableName);
				$userRes = $userModel->where($user)->select();
				if($userRes)
				{
					$userNew['user_Id'] = $arr['user_Id'];
					$saveShop = $userModel->data( $use )->where( $userNew )->save();
					if($saveShop)
					{
						return 'success';
					}else
					{
						return 'err';//密码修改失败
					}
				}else
				{
					return 'oldErr';//旧密码错误
				}
	        }
	        else{
	            return 'error';//两次新密码不一样
	        }
        }else
        {
        	return 'onEq';//验证码不正确
        }
	}

	/*修改银行卡*/
	public function bankCardIn($arr)
	{
        $u['user_Id'] = $arr['user_Id'];
        $user['banktype'] = $arr['banktype'];
        $user['card'] = $arr['card'];
        $user['name'] = $arr['name'];
        $user['idcard'] = $arr['idcard'];
        $user['openbank'] = $arr['openbank'];

		$userModel = M($this->tableName);
		$userRes = $userModel->where($u)->select();
		if($userRes)
		{
			$saveShop = $userModel->data( $user )->where( $u )->save();
			if($saveShop)
			{
				return 'success';
			}else
			{
				return 'err';//修改失败
			}
		}else
		{
			return 'oldErr';//用户不存在
		}
	}

}